package com.qf.util;

import java.util.Properties;

/**
 * @author mkr
 * @version V1.0
 * @Project spring_demo
 * @Package com.qf.util
 * @Description:
 * @Date 2022/7/10 20:41
 */
public class BeanFactory {

    private Properties properties = new Properties();

    public BeanFactory() {
    }

    public BeanFactory(String config) throws Exception {
        //加载配置文件
        properties.load(BeanFactory.class.getResourceAsStream(config));
    }

    public  Object getBean(String beanName) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        //获取配置路径
        String classPath = properties.getProperty(beanName);//userService=com.qf.service.impl.UserServiceImpl
        if (classPath != null) {
            //反射加载类对象
            Class clazz = Class.forName(classPath);
            //返回反射对象
            return clazz.newInstance();
        }
        return null;
    }
}
